package com.scau402.system1.controller;

import com.scau402.common.annotation.Log;
import com.scau402.common.core.controller.BaseController;
import com.scau402.common.core.domain.AjaxResult;
import com.scau402.common.core.page.TableDataInfo;
import com.scau402.common.enums.BusinessType;
import com.scau402.common.utils.SecurityUtils;
import com.scau402.common.utils.poi.ExcelUtil;
import com.scau402.system1.domain.dto.InboundDTO;
import com.scau402.system1.domain.query.InboundJadeQuery;
import com.scau402.system1.domain.query.WmsInboundOrderQuery;
import com.scau402.system1.domain.query.WmsOutboundOrderQuery;
import com.scau402.system1.domain.vo.*;
import com.scau402.system1.mapper.WmsWarehouseStorageMapper;
import com.scau402.system1.repository.IWmsInboundOrderRepository;
import com.scau402.system1.service.IWmsInboundOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 商品入库单ControllerV2
 *
 * @author
 * @since 2023-12-23
 */
@RestController
@RequestMapping("/v2/wms-inbound-order")
public class WmsInboundOrderControllerV2 extends BaseController {

    @Autowired
    private IWmsInboundOrderService wmsInboundOrderService;
    @Autowired
    private IWmsInboundOrderRepository inboundOrderRepo;
    @Autowired
    private WmsWarehouseStorageMapper storageMapper;

    /**
     * 查询商品入库单列表
     */
    @PreAuthorize("@ss.hasPermi('system:inboundOrder:list')")
    @GetMapping("/list")
    public TableDataInfo list(WmsInboundOrderQuery wmsInboundOrderQuery) {
        List<WmsInboundOrderVO> wmsInboundOrderBOS = wmsInboundOrderService.selectWmsInboundOrderList(wmsInboundOrderQuery);
        return getDataTable(wmsInboundOrderBOS);
    }

    /**
     * 删除商品入库单
     */
    @PreAuthorize("@ss.hasPermi('system:inboundOrder:remove')")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return wmsInboundOrderService.deleteWmsInboundOrderByIds(ids);
    }

    /**
     * 查询入库货品列表
     */
    // TODO  2024/1/4 23:59: 未完成
    @PreAuthorize("@ss.hasPermi('system:inboundOrder:list')")
    @GetMapping("/inList")
    public TableDataInfo inList(InboundJadeQuery inboundJadeQuery) {
        List<WmsInboundJadeVO> inboundJadeVOS = wmsInboundOrderService.selectInboundJadeList(inboundJadeQuery);
        return getDataTable(inboundJadeVOS);
    }

    /**
     * 采借入库
     */
    @PreAuthorize("@ss.hasPermi('system:inboundOrder:add')")
    @Log(title = "商品入库单", businessType = BusinessType.INSERT)
    @PostMapping
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult add(@RequestBody @Validated InboundDTO inboundDTO) {
        return wmsInboundOrderService.add(inboundDTO);
    }

    /**
     * 合并入库单
     */
    @PreAuthorize("@ss.hasPermi('system:inboundOrder:add')")
    @Log(title = "合并入库单", businessType = BusinessType.INSERT)
    @PostMapping("/merge")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult merge(@RequestBody List<Long> inboundOrderIds) {
        wmsInboundOrderService.merge(inboundOrderIds);
        return success();
    }

    /**
     * 获取入库单编码
     */
    @PreAuthorize("@ss.hasPermi('system:inboundOrder:add')")
    @GetMapping("/getInboundOrderCode")
    public String getInboundOrderCode() {
        return wmsInboundOrderService.getNewCode(SecurityUtils.getLoginUser().getUser());
    }

    /**
     * 导出商品入库单列表
     * @param response 响应数据
     * @param orderCodes 入库单号列表
     * @since 2024/2/17
     */
    @PreAuthorize("@ss.hasPermi('system:inboundOrder:export')")
    @Log(title = "商品入库单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestParam List<String> orderCodes) {
        List<WmsInboundOrderExportVO> wmsInboundOrderVOS = inboundOrderRepo.findListByOrderCodes(orderCodes);
        ExcelUtil<WmsInboundOrderExportVO> util = new ExcelUtil<WmsInboundOrderExportVO>(WmsInboundOrderExportVO.class);
        util.exportExcel(response, wmsInboundOrderVOS, "商品入库单数据", "入库单数据");
    }

    /**
     * 导出商品入库单详情列表
     * @param response 响应数据
     * @param orderCodes 入库单号列表
     * @since 2024/2/17
     */
    @PreAuthorize("@ss.hasPermi('system:inboundOrder:export')")
    @Log(title = "商品入库单", businessType = BusinessType.EXPORT)
    @PostMapping("/export/detail")
    public void exportDetail(HttpServletResponse response, @RequestParam List<String> orderCodes) {
        List<WmsInboundOrderDetailExportVO> wmsInboundDetailVOS = inboundOrderRepo.findDetailListByOrderCodes(orderCodes);
        ExcelUtil<WmsInboundOrderDetailExportVO> util = new ExcelUtil<WmsInboundOrderDetailExportVO>(WmsInboundOrderDetailExportVO.class);
        util.exportExcel(response, wmsInboundDetailVOS, "商品入库单详情数据", "入库单详情数据");
    }

}
